//
//  UserOrder.swift
//  SitPosition
//
//  Created by apple on 2019/2/15.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation

/**
 * 支付宝代理
 */
protocol AliPayDelegate : class {
    
    // 支付宝响应回调
    func alipayResponse(_ resultDict: [AnyHashable : Any]?) -> Void
}


/**
 * 用户待支付订单模型（app不创建订单）
 */
struct UserPendingOrder : Codable {
    
    let id: String      // 订单id
    let userId: String  // 用户id
    let amount: Double  // 订单金额，单位元
    
    private enum CodingKeys: String, CodingKey {
        case id
        case userId
        case amount
    }
}


/**
 * 用户订单支付参数模型（获取支付相关参数）
 */
protocol UserOrderPayParams : Codable { }

struct UserOrderWechatPayParams : UserOrderPayParams {
    
    struct WechatPay : Codable {
        let code: Int64             //
        let appid: String           //
        let noncestr: String        //
        let package: String         //
        let partnerid: String       //
        let prepayid: String        //
        let sign: String            //
        let timestamp: Int64?       // 时间戳，单位秒
        
        private enum CodingKeys: String, CodingKey {
            case code
            case appid
            case noncestr
            case package
            case partnerid
            case prepayid
            case sign
            case timestamp
        }
        
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            self.code = try container.decode(Int64.self, forKey: .code)
            self.appid = try container.decode(String.self, forKey: .appid)
            self.noncestr = try container.decode(String.self, forKey: .noncestr)
            self.package = try container.decode(String.self, forKey: .package)
            self.partnerid = try container.decode(String.self, forKey: .partnerid)
            if let n = try? container.decode(Int64.self, forKey: .prepayid) {
                self.prepayid = String(n)
            } else if let s = try? container.decode(String.self, forKey: .prepayid) {
                self.prepayid = s
            } else {
                throw SPError.unexpectedServerResponse
            }
            self.sign = try container.decode(String.self, forKey: .sign)
            self.timestamp = try container.decode(Int64.self, forKey: .timestamp)
        }
    }
    
    let id: String      // 订单id
    let userId: String  // 用户id
    let amount: Double  // 订单金额，单位元
    let appPay: WechatPay // 微信app支付参数
    
}


struct UserOrderAliPayParams : UserOrderPayParams {
    
    struct AliPay : Codable {
        let url: String             // 支付的链接URL
        let referer: String?        // 头信息
        let payMethod: Int?         // 支付方式: 1-支付宝，2-微信
    }
    
    let id: String      // 订单id
    let userId: String  // 用户id
    let amount: Double  // 订单金额，单位元
    let payUrl: AliPay  // url支付参数
}
